<?php
include 'admin_passport_module_lib.php';

if ($s_Action == 'insert' || $s_Action == 'update') {
    
    // <editor-fold defaultstate="collapsed" desc="Get Common Condition | Insert - Update">
    if ($f_moduleid == '') { raise_error('No ID.'); }
    
    $a_row = array(
        'modulename'   => $f_modulename  ,
        'moduletype'   => $f_moduletype  ,
        'modulestatus' => $f_modulestatus,
        'note'         => $f_note        ,
        'updaterid'    => $s_UserID   ,
        'updatedate'   => $f_updatedate  ,
    );
    
    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_module);
    $db_query  = $db_query . " where moduleid = '$f_moduleid'";
    $db_result = db_query($db_conn, $db_query);
    // </editor-fold>
}

if ($s_Action == 'insert') {
    
    // <editor-fold defaultstate="collapsed" desc="Update Relative Insert-Data">
    if($d_passport_module = db_fetch($db_result)){ raise_error('Module ID is NOT Available.'); }

    $a_row['moduleid'   ] = $f_moduleid;
    $a_row['creatorid'  ] = $s_UserID;
    $a_row['insertdate' ] = $f_insertdate;

    db_insert_row($db_conn, $a_row, 'passport_module');
    // </editor-fold>
}
else if ($s_Action == 'update') {
    
    // <editor-fold defaultstate="collapsed" desc="Update Relative Update-Data">
    if(!($d_passport_module = db_fetch($db_result))){ raise_error('Select None.'); }
    
    db_update_row($db_conn, $a_row, 'passport_module', "moduleid = '$f_moduleid'");
    db_delete_row($db_conn, 'passport_module_page', "moduleid = '$f_moduleid'");
    
    // <editor-fold defaultstate="collapsed" desc="Update Module Action">
    $f_actionids = isset($_REQUEST['actionids']) ? trim($_REQUEST['actionids']) : '';
    
    if ($f_actionids) { $f_actionids = explode(" ,", $f_actionids); }
    else              { $f_actionids = array ( );                   }
    
    $a_tmp_old_moduleaction = $a_tmp_delete_moduleaction = array();
    
    $db_query  = "
        select  actionid, moduleactionid
        from    passport_module_action
        where   moduleid = '$f_moduleid'";

    $db_result = db_query($db_conn, $db_query);
    
    while ($d_row = db_fetch($db_result)) {
        
        if (in_array($d_row->actionid, $f_actionids)) {
            $a_tmp_old_moduleaction[$d_row->actionid] = $d_row->actionid;
        }
        else {
            $a_tmp_delete_moduleaction[$d_row->moduleactionid] = $d_row->moduleactionid;
        }
    }
    
    $_REQUEST['actionids'] = array_diff($f_actionids, $a_tmp_old_moduleaction);

    $_REQUEST['actionids'] = !empty($_REQUEST['actionids']) ? implode(" ,", $_REQUEST['actionids']) : '';

    db_delete_row($db_conn, 'passport_module_action', "moduleactionid in ('" . implode("', '", $a_tmp_delete_moduleaction) . "') ");
    db_delete_row($db_conn, 'passport_permission'   , "moduleactionid in ('" . implode("', '", $a_tmp_delete_moduleaction) . "') ");
    // </editor-fold>
    
    // </editor-fold>
}

if ($s_Action == 'insert' || $s_Action == 'update') {

    // <editor-fold defaultstate="collapsed" desc="Update Relative Common-Data">
    $f_actionids = isset($_REQUEST['actionids']) ? trim($_REQUEST['actionids']) : '';
    $f_pageids   = isset($_REQUEST['pageids'  ]) ? trim($_REQUEST['pageids'  ]) : '';
    
    if ($f_actionids) { $f_actionids = explode(" ,", $f_actionids); }
    else              { $f_actionids = array ( );                   }
    
    if ($f_pageids) { $f_pageids = explode(" ,", $f_pageids); }
    else            { $f_pageids = array ( );                 }

    // <editor-fold defaultstate="collapsed" desc="Insert Actions">
    if(!empty($f_actionids)) {
        
        $a_rows = array();

        foreach($f_actionids as $f_actionid) {
            
            $a_rows[] = array(
                'moduleid'          => $f_moduleid          ,
                'actionid'          => $f_actionid          ,
                'moduleactionstatus'=> $f_modulestatus      ,
                'creatorid'         => $s_UserID            ,
                'insertdate'        => $f_insertdate        ,
                'updaterid'         => $s_UserID            ,
                'updatedate'        => $f_updatedate        ,
            );
        }

        db_insert_row($db_conn, $a_rows, 'passport_module_action');
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Insert Pages">
    if(!empty($f_pageids)) {
        
        $a_rows = array();

        foreach($f_pageids as $f_pageid) {
            $a_rows[] = array (
                'moduleid'   => $f_moduleid,
                'pageid'     => strtoupper($f_pageid),
                'creatorid'  => $s_UserID,
                'insertdate' => $f_insertdate,
                'updaterid'  => $s_UserID,
                'updatedate' => $f_updatedate,
            );
        }
        
        db_insert_row($db_conn, $a_rows, 'passport_module_page');
    }
    // </editor-fold>
    
    // </editor-fold>
}
else if ($s_Action == 'delete') {    
    
    // <editor-fold defaultstate="collapsed" desc="Delete">
    $f_moduleids = array($f_moduleid);

    if (isset($_REQUEST['moduleids'] )) { $f_moduleids = $_REQUEST['moduleids'] ; }

    $s_Where = "moduleid in ('" . implode("','", $f_moduleids) . "')";

    db_delete_row($db_conn, 'passport_module', $s_Where);
    db_delete_row($db_conn, 'passport_module_action', $s_Where);
    // </editor-fold>
}
else {
    raise_error('No Action.');
}

// <editor-fold defaultstate="collapsed" desc="Update Cache">
access_cachefile($s_Cache_Path, 'passport_pages', array('db_conn' => $db_conn, 'generate' => true));
// </editor-fold>

local_redirect('admin_passport_module_list.php?' . $s_QueryString_Page);
?>